﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using System.IO;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using System.IO.Ports;
using weiqijiance;
using ZedGraph;
using CCWin;

namespace weiqijiance
{
    public partial class JDNeiZu : CCSkinMain
    {
        public mainform m_mainform;
        public Thread t1;
        public Thread t2;

        public float fli;
        public float fspeed;
        public bool btingzhi;
        public float[][] fForce;
        public float[][] ftime;
        public float kgl;
        public float fSpeedStrat;
        public float fSpeedEnd;
        public float DIW;

        public double time50;
        public double time40;
        public double time30;
        public double time20;
        public double time10;

        public double neizu50;
        public double neizu40;
        public double neizu30;
        public double neizu20;
        public double neizu10;

        public int icoungcsj;

        double[] dtime = new double[10000];
        double[] dSP = new double[10000];
        double[] dPW = new double[10000];
        double[] dFC = new double[10000];

        DateTime dstartgcsj;
        public int igcsj;

        public float flilingwei;
        public double dymax;
        DateTime dpinlv;

        public float m_fspeed;
        public float fForce1;

        public string sASMJGXZ;//是否进行结果修正
        public int iASMtdz;//ASM信号左通道
        public int iASMtdy;//ASM信号右通道
        public int iASMtdh;//ASM信号右通道
        public int iASMHWQ;//ASM到位红外

        public int iASMKZJSS;//ASM举升升
        public int iASMKZJSJ;//ASM举升降
        public int iASMSDtd;//车速信号接在那个通道
        //红外取反
        public bool[] bhongwaiqf = new bool[16] { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false };//红外是否取反
        //红外定位时间
        public int[] ihongwaidwsj = new int[16] { 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000 };//红外定位时间单位毫秒



        public JDNeiZu()
        {
            InitializeComponent();
           
            dymax = 0;
            igcsj = 0;
            DIW = 0;

          
           
           
            icoungcsj = 0;
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

            x = this.Width;
            y = this.Height;
            setTag(this);

        }

        #region 控件大小随窗体大小等比例缩放
        private float x;//定义当前窗体的宽度
        private float y;//定义当前窗体的高度
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
            {
                //获取控件的Tag属性值，并分割后存储字符串数组
                if (con.Tag != null)
                {
                    string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);//宽度
                    con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);//高度
                    con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);//左边距
                    con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);//顶边距
                    Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }
            }
        }
        #endregion

        private delegate void FlushClient();//代理
       
        private void xianshiSpeed()
        {
            if (this.text_sudu.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshiSpeed);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_sudu.Text = string.Format(@"{0}", fspeed.ToString("0.00"));
            }
        }

        private void xianshitime50()
        {
            if (this.text_sudu.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshitime50);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_time50.Text = string.Format(@"{0}", time50);
            }
        }

        private void xianshitime40()
        {
            if (this.text_sudu.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshitime40);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_time40.Text = string.Format(@"{0}", time40);
            }
        }

        private void xianshitime30()
        {
            if (this.text_sudu.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshitime30);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_time30.Text = string.Format(@"{0}", time30);
            }
        }

        private void xianshitime20()
        {
            if (this.text_sudu.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshitime20);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_time20.Text = string.Format(@"{0}", time20);
            }
        }
        private void xianshitime10()
        {
            if (this.text_sudu.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshitime10);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_time10.Text = string.Format(@"{0}", time10);
            }
        }



        private void xianshineizu50()
        {
            if (this.text_sudu.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshineizu50);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_neizu50.Text = string.Format(@"{0}", neizu50.ToString("0.00"));
            }
        }

        private void xianshineizu40()
        {
            if (this.text_sudu.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshineizu40);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_neizu40.Text = string.Format(@"{0}", neizu40.ToString("0.00"));
            }
        }

        private void xianshineizu30()
        {
            if (this.text_sudu.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshineizu30);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_neizu30.Text = string.Format(@"{0}", neizu30.ToString("0.00"));
            }
        }

        private void xianshineizu20()
        {
            if (this.text_sudu.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshineizu20);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_neizu20.Text = string.Format(@"{0}", neizu20.ToString("0.00"));
            }
        }

        private void xianshineizu10()
        {
            if (this.text_sudu.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshineizu10);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_neizu10.Text = string.Format(@"{0}", neizu10.ToString("0.00"));
            }
        }
        public void WriteGraph(ZedGraphControl zgc, double[] izdz, double[] izdy, int icount)
        {
            double imax = 0;
            for (int i = 0; i < icount; i++)
            {
                if (imax < izdz[i])
                {
                    imax = izdz[i];
                }
            }
            imax = imax * 1.1;
            for (int i = 0; i < icount; i++)
            {

                if (imax < izdy[i])
                {
                    imax = izdy[i];
                }

            }
            imax = imax * 1.1;

            GraphPane myPane = zgc.GraphPane;
            myPane.Fill = new Fill(Color.White, Color.Aqua, 45.0f);
            myPane.Legend.Fill = new Fill(Color.Gainsboro);
            myPane.Chart.Fill = new Fill(Color.Gainsboro);

            myPane.CurveList.Clear();

            myPane.Title.Text = string.Format("曲线");

            myPane.XAxis.Title.Text = "时间(s)";
            myPane.YAxis.Title.Text = "速度(km/h)";

            myPane.YAxis.MajorGrid.IsVisible = false;
            myPane.YAxis.MinorTic.Size = 0f;

            myPane.YAxis.Scale.Min = 0;
            myPane.YAxis.Scale.Max = imax + 0;
            myPane.YAxis.Scale.MajorStep = 0.5;

            zgc.IsShowPointValues = true;//数据节点
            string ss0 = string.Format("速度");
            string ss1 = string.Format("功率");

            PointPairList list0 = new PointPairList();
            PointPairList list1 = new PointPairList();


            LineItem myCurve0 = myPane.AddCurve(ss0, list0, Color.Blue,
                                    SymbolType.None);
            LineItem myCurve1 = myPane.AddCurve(ss1, list1, Color.Red,
                                    SymbolType.None);
            myCurve0.Line.Width = 1.7F;
            myCurve1.Line.Width = 1.7F;

            for (int i = 0; i < icount; i++)
            {
                list0.Add(i, izdz[i]);
                list1.Add(i, izdy[i]);
            }
            zgc.AxisChange();
            zgc.Invalidate();
        }

       
        public void gettongdao()
        {

            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\串口及通道设置.ini";
            string path1 = string.Format(@"{0}", path);
            m_mainform.Ini(path1);

            ///读取检定数据

            for (int i = 0; i < 10; i++)
            {
                string sgetname = m_mainform.ReadValue("通道设置", string.Format(@"模拟信号{0}", i + 1));
                if (sgetname.Contains("测功力左"))
                {
                    iASMtdz = i;
                }

                if (sgetname.Contains("测功力右"))
                {
                    iASMtdy = i;
                }

                if (sgetname.Contains("测功力后"))
                {
                    iASMtdh = i;
                }
            }

            for (int i = 0; i < 8; i++)
            {
                string smaichong = m_mainform.ReadValue("通道设置", string.Format(@"速度{0}", i + 1));
                if (smaichong.Contains("速度"))
                {
                    iASMSDtd = i;
                }

            }

            for (int i = 0; i < 16; i++)
            {
                string shongwai = m_mainform.ReadValue("通道设置", string.Format(@"红外{0}", i + 1));
                if (shongwai.Contains("测功机到位"))
                {
                    iASMHWQ = i;
                }


                shongwai = m_mainform.ReadValue("通道设置", string.Format(@"红外定位时间{0}", i + 1));
                int.TryParse(shongwai, out ihongwaidwsj[i]);


                shongwai = m_mainform.ReadValue("通道设置", string.Format(@"红外是否取反{0}", i + 1));
                if (shongwai.Contains("是"))
                {
                    bhongwaiqf[i] = true;
                }
                else
                {
                    bhongwaiqf[i] = false;
                }
            }
            ///控制
            for (int i = 0; i < 8; i++)
            {

                string shongwai = m_mainform.ReadValue("通道设置", string.Format(@"控制{0}", i + 1));
                if (shongwai.Contains("测功举升升"))
                {
                    iASMKZJSS = i;
                }
                if (shongwai.Contains("测功举升降"))
                {
                    iASMKZJSJ = i;
                }
            }
        }
       

        


        public void Thread1()
        {
            try
            {
                testneizu();
            }
            catch
            {
 
            }
        }
        public void Thread2()
        {
            


        }
        private double DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            double dateDiff = 0;
            TimeSpan ts = DateTime1.Subtract(DateTime2).Duration();
            dateDiff = ts.Days * 60 * 60 * 60 * 1000 + ts.Hours * 60 * 60 * 1000 + ts.Minutes * 60 * 1000 + ts.Seconds * 1000 + ts.Milliseconds;
            return dateDiff;
        }
        public void testneizu()//内阻
        {

            igcsj = 0;
                    m_mainform.DAC.PutVol(0);
                    Thread.Sleep(1500);
                    m_mainform.BianPinQi(true);
                    Thread.Sleep(3000);
                    m_mainform.BianPinQiSpeed(256*30);//设置频率 256 对应2.56
                    dpinlv = DateTime.Now;
                    while(true)
                    {
                        Thread.Sleep(20);
                        m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                        float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                        float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                        float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                        fForce1 = fzou + fyou + fhou;
                        if (fForce1 < 0)
                        {
                            fForce1 = 0;
                        }
                        if (m_fspeed > 56)
                        {
                            m_mainform.BianPinQi(false); ;//关闭电源电源
                            break;
                        }
                       
                        fspeed = m_fspeed;
                        xianshiSpeed();

                        //过程数据
                        if (DateDiff(DateTime.Now, dpinlv) > 500)
                        {
                            m_mainform.baocunriji("内阻速度采样值", string.Format("{0}", m_mainform.DAC.fTIData[iASMSDtd]));
                            dSP[igcsj] = (double)fspeed;
                            dPW[igcsj] = fForce1 * fspeed / 360;
                            dtime[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;

                            dFC[igcsj] = fForce1;

                            WriteGraph(zedGraphControl1, dSP, dPW, igcsj + 1);

                            igcsj++;
                            dpinlv = DateTime.Now;
                        }



                    }
                    DateTime dstart50;
                    DateTime dend50;


                    dpinlv = DateTime.Now;
                    while (true)
                    {
                        Thread.Sleep(20);
                        m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                        float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                        float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                        float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                        fForce1 = fzou + fyou + fhou;
                        if (fForce1 < 0)
                        {
                            fForce1 = 0;
                        }
                        if (m_fspeed <= 54)
                        {
                            dstart50 = DateTime.Now;//记录开始滑行时间
                            dstartgcsj = DateTime.Now;
                            break;
                        }
                        fspeed = m_fspeed;
                        xianshiSpeed();
                        //过程数据
                        if (DateDiff(DateTime.Now, dpinlv) > 500)
                        {
                            m_mainform.baocunriji("内阻速度采样值", string.Format("{0}", m_mainform.DAC.fTIData[iASMSDtd]));
                            dSP[igcsj] = (double)fspeed;
                            dPW[igcsj] = fForce1 * fspeed / 360;
                            dtime[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;

                            dFC[igcsj] = fForce1;
                           
                            WriteGraph(zedGraphControl1,  dSP, dPW, igcsj + 1);

                            igcsj++;
                            dpinlv = DateTime.Now;
                        }

                    }
                    while (true)
                    {
                        Thread.Sleep(20);
                        m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                        float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                        float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                        float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                        fForce1 = fzou + fyou + fhou;
                        if (fForce1 < 0)
                        {
                            fForce1 = 0;
                        }
                        if (m_fspeed <= 46)
                        {
                            dend50 = DateTime.Now;//记录开始滑行时间
                            break;
                        }
                        fspeed = m_fspeed;
                        xianshiSpeed();

                        //过程数据
                        if (DateDiff(DateTime.Now, dpinlv) > 500)
                        {
                            m_mainform.baocunriji("内阻速度采样值", string.Format("{0}", m_mainform.DAC.fTIData[iASMSDtd]));
                            dSP[igcsj] = (double)fspeed;
                            dPW[igcsj] = fForce1 * fspeed / 360;
                            dtime[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;

                            dFC[igcsj] = fForce1;
                            WriteGraph(zedGraphControl1,  dSP, dPW, igcsj + 1);
                            igcsj++;
                            dpinlv = DateTime.Now;
                        }
                    }
            //50结束
                    double dnei50 = 0;
                    dnei50 = 1000 * 0.030864 * DIW / DateDiff(dend50, dstart50);
                    time50 = DateDiff(dend50, dstart50) * 0.001;
                    neizu50 = dnei50 * m_mainform.m_fneizu;
                    xianshitime50();
                    xianshineizu50();

                    DateTime dstart40;
                    DateTime dend40;
                    while (true)
                    {
                        Thread.Sleep(20);
                        m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                        float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                        float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                        float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                        fForce1 = fzou + fyou + fhou;
                        if (fForce1 < 0)
                        {
                            fForce1 = 0;
                        }
                        if (m_fspeed <= 44)
                        {
                            dstart40 = DateTime.Now;//记录开始滑行时间
                            break;
                        }
                        fspeed = m_fspeed;
                        xianshiSpeed();
                        //过程数据
                        if (DateDiff(DateTime.Now, dpinlv) > 500)
                        {
                            m_mainform.baocunriji("内阻速度采样值", string.Format("{0}", m_mainform.DAC.fTIData[iASMSDtd]));
                            dSP[igcsj] = (double)fspeed;
                            dPW[igcsj] = fForce1 * fspeed / 360;
                            dtime[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;
                            dFC[igcsj] = fForce1;
                            WriteGraph(zedGraphControl1,  dSP, dPW, igcsj + 1);
                            igcsj++;
                            dpinlv = DateTime.Now;
                        }
                    }
                    while (true)
                    {
                        Thread.Sleep(20);
                        m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                        float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                        float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                        float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                        fForce1 = fzou + fyou + fhou;
                        if (fForce1 < 0)
                        {
                            fForce1 = 0;
                        }
                        if (m_fspeed <= 36)
                        {
                            dend40 = DateTime.Now;//记录开始滑行时间
                            break;
                        }
                        fspeed = m_fspeed;
                        xianshiSpeed();
                        //过程数据
                        if (DateDiff(DateTime.Now, dpinlv) > 500)
                        {
                            m_mainform.baocunriji("内阻速度采样值", string.Format("{0}", m_mainform.DAC.fTIData[iASMSDtd]));
                            dSP[igcsj] = (double)fspeed;
                            dPW[igcsj] = fForce1 * fspeed / 360;
                            dtime[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;
                            dFC[igcsj] = fForce1;
                            WriteGraph(zedGraphControl1,  dSP, dPW, igcsj + 1);
                            igcsj++;
                            dpinlv = DateTime.Now;
                        }
                    }
                    //40结束
                    double dnei40 = 0;
                    dnei40 = 1000 * 0.030864 * DIW / DateDiff(dend40, dstart40);

                    time40 = DateDiff(dend40, dstart40) * 0.001; 
                   
                    neizu40 = dnei40 * m_mainform.m_fneizu;
                    xianshitime40();
                    xianshineizu40();

                    DateTime dstart30;
                    DateTime dend30;
                    while (true)
                    {
                        Thread.Sleep(20);
                        m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                        float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                        float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                        float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                        fForce1 = fzou + fyou + fhou;
                        if (fForce1 < 0)
                        {
                            fForce1 = 0;
                        }
                        if (m_fspeed <= 34)
                        {
                            dstart30 = DateTime.Now;//记录开始滑行时间
                            break;
                        }
                        fspeed = m_fspeed;
                        xianshiSpeed();
                        //过程数据
                        if (DateDiff(DateTime.Now, dpinlv) > 500)
                        {
                            m_mainform.baocunriji("内阻速度采样值", string.Format("{0}", m_mainform.DAC.fTIData[iASMSDtd]));
                            dSP[igcsj] = (double)fspeed;
                            dPW[igcsj] = fForce1 * fspeed / 360;
                            dtime[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;
                            dFC[igcsj] = fForce1;
                            WriteGraph(zedGraphControl1,  dSP, dPW, igcsj + 1);
                            igcsj++;
                            dpinlv = DateTime.Now;
                        }
                    }
                    while (true)
                    {
                        Thread.Sleep(20);
                        m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                        float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                        float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                        float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                        fForce1 = fzou + fyou + fhou;
                        if (fForce1 < 0)
                        {
                            fForce1 = 0;
                        }
                        if (m_fspeed <= 26)
                        {
                            dend30 = DateTime.Now;//记录开始滑行时间
                            break;
                        }
                        fspeed = m_fspeed;
                        xianshiSpeed();
                        //过程数据
                        if (DateDiff(DateTime.Now, dpinlv) > 500)
                        {
                            m_mainform.baocunriji("内阻速度采样值", string.Format("{0}", m_mainform.DAC.fTIData[iASMSDtd]));
                            dSP[igcsj] = (double)fspeed;
                            dPW[igcsj] = fForce1 * fspeed / 360;
                            dtime[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;
                            dFC[igcsj] = fForce1;
                            WriteGraph(zedGraphControl1,  dSP, dPW, igcsj + 1);
                            igcsj++;
                            dpinlv = DateTime.Now;
                        }
                    }
                    //30结束
                    double dnei30 = 0;
                    dnei30 = 1000 * 0.030864 * DIW / DateDiff(dend30, dstart30) ;

                    time30 = DateDiff(dend30, dstart30) * 0.001;
                    neizu30 = dnei30 * m_mainform.m_fneizu;
                    xianshitime30();
                    xianshineizu30();

                    DateTime dstart20;
                    DateTime dend20;
                    while (true)
                    {
                        Thread.Sleep(20);
                        m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                        float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                        float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                        float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                        fForce1 = fzou + fyou + fhou;
                        if (fForce1 < 0)
                        {
                            fForce1 = 0;
                        }
                        if (m_fspeed <= 24)
                        {
                            dstart20 = DateTime.Now;//记录开始滑行时间
                            break;
                        }
                        fspeed = m_fspeed;
                        xianshiSpeed();
                        //过程数据
                        if (DateDiff(DateTime.Now, dpinlv) > 500)
                        {
                            m_mainform.baocunriji("内阻速度采样值", string.Format("{0}", m_mainform.DAC.fTIData[iASMSDtd]));
                            dSP[igcsj] = (double)fspeed;
                            dPW[igcsj] = fForce1 * fspeed / 360;
                            dtime[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;
                            dFC[igcsj] = fForce1;
                            WriteGraph(zedGraphControl1,  dSP, dPW, igcsj + 1);
                            igcsj++;
                            dpinlv = DateTime.Now;
                        }
                    }
                    while (true)
                    {
                        Thread.Sleep(20);
                        m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                        float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                        float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                        float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                        fForce1 = fzou + fyou + fhou;
                        if (fForce1 < 0)
                        {
                            fForce1 = 0;
                        }
                        if (m_fspeed <= 16)
                        {
                            dend20 = DateTime.Now;//记录开始滑行时间
                            break;
                        }
                        fspeed = m_fspeed;
                        xianshiSpeed();
                        //过程数据
                        if (DateDiff(DateTime.Now, dpinlv) > 500)
                        {
                            m_mainform.baocunriji("内阻速度采样值", string.Format("{0}", m_mainform.DAC.fTIData[iASMSDtd]));
                            dSP[igcsj] = (double)fspeed;
                            dPW[igcsj] = fForce1 * fspeed / 360;
                            dtime[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;

                            dFC[igcsj] = fForce1;
                            WriteGraph(zedGraphControl1,  dSP, dPW, igcsj + 1);
                            igcsj++;
                            dpinlv = DateTime.Now;
                        }
                    }
                    //20结束
                    double dnei20 = 0;
                    dnei20 = 1000 * 0.030864 * DIW / DateDiff(dend20, dstart20);

                    time20 = (DateDiff(dend20, dstart20) * 0.001); 
                    neizu20 = dnei20 * m_mainform.m_fneizu;
                    xianshitime20();
                    xianshineizu20();

                    DateTime dstart10;
                    DateTime dend10;
                    while (true)
                    {
                        Thread.Sleep(20);
                        m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                        float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                        float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                        float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                        fForce1 = fzou + fyou + fhou;
                        if (fForce1 < 0)
                        {
                            fForce1 = 0;
                        }
                        if (m_fspeed <= 14)
                        {
                            dstart10 = DateTime.Now;//记录开始滑行时间
                            break;
                        }
                        fspeed = m_fspeed;
                        xianshiSpeed();
                        //过程数据
                        if (DateDiff(DateTime.Now, dpinlv) > 500)
                        {
                            m_mainform.baocunriji("内阻速度采样值", string.Format("{0}", m_mainform.DAC.fTIData[iASMSDtd]));
                            dSP[igcsj] = (double)fspeed;
                            dPW[igcsj] = fForce1 * fspeed / 360;
                            dtime[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;
                            dFC[igcsj] = fForce1;
                            WriteGraph(zedGraphControl1,  dSP, dPW, igcsj + 1);
                            igcsj++;
                            dpinlv = DateTime.Now;
                        }
                    }
                    while (true)
                    {
                        Thread.Sleep(20);
                        m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                        float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                        float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                        float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                        fForce1 = fzou + fyou + fhou;
                        if (fForce1 < 0)
                        {
                            fForce1 = 0;
                        }
                        if (m_fspeed <= 6)
                        {
                            dend10 = DateTime.Now;//记录开始滑行时间
                            break;
                        }
                        fspeed = m_fspeed;
                        xianshiSpeed();
                        //过程数据
                        if (DateDiff(DateTime.Now, dpinlv) > 500)
                        {
                            m_mainform.baocunriji("内阻速度采样值", string.Format("{0}", m_mainform.DAC.fTIData[iASMSDtd]));
                            dSP[igcsj] = (double)fspeed;
                            dPW[igcsj] = fForce1 * fspeed / 360;
                            dtime[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;

                            dFC[igcsj] = fForce1;
                            WriteGraph(zedGraphControl1,  dSP, dPW, igcsj + 1);
                            igcsj++;
                            dpinlv = DateTime.Now;
                        }
                    }
                    //10结束
                    double dnei10 = 0;
                    dnei10 = 1000 * 0.030864 * DIW / DateDiff(dend10, dstart10);

                    time10 = DateDiff(dend10, dstart10) * 0.001; 
                    neizu10 = dnei10 * m_mainform.m_fneizu;
                    xianshitime10();
                    xianshineizu10();





            ///记录检定数据
                    string path = "";
                    path = System.Windows.Forms.Application.StartupPath + path + "\\设备检定结果.ini";
                    string path1 = string.Format(@"{0}", path);
                    m_mainform.Ini(path1);

                    m_mainform.Writue("内阻检定", "50滑行时间", text_time50.Text);
                    m_mainform.Writue("内阻检定", "50内阻", text_neizu50.Text);
                    m_mainform.Writue("内阻检定", "50开始滑行", dstart50.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    m_mainform.Writue("内阻检定", "50结束滑行", dend50.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    m_mainform.Writue("内阻检定", "40滑行时间", text_time40.Text);
                    m_mainform.Writue("内阻检定", "40内阻", text_neizu40.Text);
                    m_mainform.Writue("内阻检定", "40开始滑行", dstart40.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    m_mainform.Writue("内阻检定", "40结束滑行", dend40.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    m_mainform.Writue("内阻检定", "30滑行时间", text_time30.Text);
                    m_mainform.Writue("内阻检定", "30内阻", text_neizu30.Text);
                    m_mainform.Writue("内阻检定", "30开始滑行", dstart30.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    m_mainform.Writue("内阻检定", "30结束滑行", dend30.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    m_mainform.Writue("内阻检定", "20滑行时间", text_time20.Text);
                    m_mainform.Writue("内阻检定", "20内阻", text_neizu20.Text);
                    m_mainform.Writue("内阻检定", "20开始滑行", dstart20.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    m_mainform.Writue("内阻检定", "20结束滑行", dend20.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    m_mainform.Writue("内阻检定", "10滑行时间", text_time10.Text);
                    m_mainform.Writue("内阻检定", "10内阻", text_neizu10.Text);
                    m_mainform.Writue("内阻检定", "10开始滑行", dstart10.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    m_mainform.Writue("内阻检定", "10结束滑行", dend10.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    m_mainform.Writue("内阻检定", "校验人", skinCom_czy.Text.Length>1?skinCom_czy.Text:"张先生");
                    while(true)
                    {
                        Thread.Sleep(20);
                        m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                        float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                        float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                        float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                        fForce1 = fzou + fyou + fhou;
                        if (fForce1 < 0)
                        {
                            fForce1 = 0;
                        }
                        fspeed = m_fspeed;
                        xianshiSpeed();

                        if (m_fspeed < 1)
                        {
                            break;
                        }
                    }
                    m_mainform.DAC.Lift(true);

            ///记录检定数据     
                    string sjieguo = string.Format(@"50滑行时间{0};
                                                    50内阻{1};
                                                    40滑行时间{2};
                                                    40内阻{3};
                                                    30滑行时间{4};
                                                    30内阻{5};
                                                    20滑行时间{6};
                                                    20内阻{7};
                                                    10滑行时间{8};
                                                    10内阻{9}",
                                                    text_time50.Text,
                                                    text_neizu50.Text,
                                                    text_time40.Text,
                                                    text_neizu40.Text,
                                                    text_time30.Text,
                                                    text_neizu30.Text,
                                                    text_time20.Text,
                                                    text_neizu20.Text,
                                                    text_time10.Text,
                                                    text_neizu10.Text);
                    string ssqlex = string.Format(@"insert into JDSJ (
                                               JDSJ_MC,
                                               JDSJ_JG,
                                               JDSJ_RQ,
                                               JDSJ_JCX) values (
                                                '{0}',
                                                '{1}',
                                                '{2}',
                                                '{3}')",
                                                 "内阻检定",
                                                 sjieguo,
                                                 DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                                 m_mainform.lineCode);
                    m_mainform.objbass.objdb.InsertDB(ssqlex);


         

        }
       

      

        private void skinBut_ksjc_Click(object sender, EventArgs e)
        {
            gettongdao();
            m_mainform.DAC.tiaoling();//调零
            m_mainform.DAC.Lift(false);
            Thread.Sleep(1500);

            ///读取检定数据
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\检测设备信息设置.ini";
            string path1 = string.Format(@"{0}", path);
            m_mainform.Ini(path1);

            float.TryParse(m_mainform.ReadValue("检测设备信息", "台架惯量"), out DIW);
            if (DIW < 1)
            {
                DIW = 900;
            }


            btingzhi = false;
            icoungcsj = 0;


          

            t1 = new Thread(new ThreadStart(Thread1));
            t1.Priority = ThreadPriority.BelowNormal;
            t1.Start();
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {

            m_mainform.DAC.PutVol(0);
            Thread.Sleep(1000);
            m_mainform.BianPinQi(false);
            btingzhi = true;
            t1.Abort();
            t1.DisableComObjectEagerCleanup();

            t2.Abort();
            t2.DisableComObjectEagerCleanup();
        }

        private void JDNeiZu_Resize(object sender, EventArgs e)
        {
            float newx = (this.Width) / x;
            float newy = (this.Height) / y;
            setControls(newx, newy, this);
        }

        private void JDNeiZu_FormClosing(object sender, FormClosingEventArgs e)
        {

            m_mainform.DAC.PutVol(0);
            Thread.Sleep(1000);
            m_mainform.BianPinQi(false);
        }

        private void JDNeiZu_Load(object sender, EventArgs e)
        {
            iniCZY("CZY");
            textBox1.Text = m_mainform.stationCode;
            textBox2.Text = m_mainform.lineCode;
            textBox3.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }
        public void iniCZY(string dataname)
        {

            string ssqlex = "";
            //ssqlex = string.Format(@"select * from {0}", dataname);
            ssqlex = string.Format(@"select * from Employee where EP_TYPE ='引车员'");
            DataTable dt = m_mainform.objbass.objdb.GetTable(ssqlex);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                skinCom_czy.Items.Add(dt.Rows[i]["EP_NAME"].ToString().Trim());
                if (i == 0)
                    skinCom_czy.Text = dt.Rows[0]["EP_NAME"].ToString().Trim();
            }
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\检测设备信息设置.ini";
            string path1 = string.Format(@"{0}", path);
            m_mainform.Ini(path1);

            float.TryParse(m_mainform.ReadValue("检测设备信息", "台架惯量"), out DIW);
            if (DIW < 1)
            {
                DIW = 900;
            }

            //读取

            iniclass objini = new iniclass();
            objini.Ini(System.Windows.Forms.Application.StartupPath + "\\设备检定结果.ini");
            model.尾气自检.jsglv objjsgl = new model.尾气自检.jsglv();
            objjsgl.hxsj1 = objini.ReadValue("内阻检定", "50滑行时间");
            objjsgl.jsgl1 = objini.ReadValue("内阻检定", "50内阻");
            objjsgl.PLTimeStart1 = objini.ReadValue("内阻检定", "50开始滑行");
            objjsgl.SlideTimeEnd1 = objini.ReadValue("内阻检定", "50结束滑行");
            objjsgl.hxsj2 = objini.ReadValue("内阻检定", "40滑行时间");
            objjsgl.jsgl2 = objini.ReadValue("内阻检定", "40内阻");
            objjsgl.PLTimeStart2 = objini.ReadValue("内阻检定", "40开始滑行");
            objjsgl.SlideTimeEnd2 = objini.ReadValue("内阻检定", "40结束滑行");
            objjsgl.hxsj3 = objini.ReadValue("内阻检定", "30滑行时间");
            objjsgl.jsgl3 = objini.ReadValue("内阻检定", "30内阻");
            objjsgl.PLTimeStart3 = objini.ReadValue("内阻检定", "30开始滑行");
            objjsgl.SlideTimeEnd3 = objini.ReadValue("内阻检定", "30结束滑行");
            objjsgl.hxsj4 = objini.ReadValue("内阻检定", "20滑行时间");
            objjsgl.jsgl4 = objini.ReadValue("内阻检定", "20内阻");
            objjsgl.PLTimeStart4 = objini.ReadValue("内阻检定", "20开始滑行");
            objjsgl.SlideTimeEnd4 = objini.ReadValue("内阻检定", "20结束滑行");
            objjsgl.hxsj5 = objini.ReadValue("内阻检定", "10滑行时间");
            objjsgl.jsgl5 = objini.ReadValue("内阻检定", "10内阻");
            objjsgl.PLTimeStart5 = objini.ReadValue("内阻检定", "10开始滑行");
            objjsgl.SlideTimeEnd5 = objini.ReadValue("内阻检定", "10结束滑行");
            objjsgl.Checker = objini.ReadValue("内阻检定", "检验员");
            objjsgl.DIW = DIW.ToString();

            m_mainform.objbass.uploadzj(objjsgl);
        }

        private void up_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.Lift(true);
        }

        private void down_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.Lift(false);
        }
    }
}
